home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12215 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  50 lines

  1. Path: mayne.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: microsecond delay
  5. Date: 29 Mar 1996 10:33:31 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4jhadrINNsim@mayne.ugrad.cs.ubc.ca>
  8. References: <14546@828046609> <4jgo7h$n7i@darkstar.UCSC.EDU>
  9. NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
  10.  
  11. In article <4jgo7h$n7i@darkstar.UCSC.EDU>, Moth <moth@cse.ucsc.edu> wrote:
  12. >In article <14546@828046609>, dan j <djacobso@cs.indiana.edu> wrote:
  13. >>We are writing a program that reads bar codes swiped on a card reader.
  14. >>
  15. >>The problem is that when we write to the device on the serial line,
  16. >>the following read is too fast for the device.  We have to delay the
  17. >>read briefly.  We can use "sleep" to delay a second, but that's too
  18. >>long.
  19. >>
  20. >>Is there any way effect delays in miliseconds without resorting to
  21. >>loops?
  22. >
  23. >void usleep(unsigned long usec);
  24.  
  25. Unfortunately, this is defined only by the BSD 4.3+ ``standard'', not POSIX and
  26. certainly not ANSI C.
  27.  
  28. If you want a POSIX.1 compliant fine-grained sleep, you have to implement it in
  29. terms of select():
  30.  
  31. #include <sys/time.h>
  32.  
  33. int microsleep(int microseconds)
  34.  
  35. {
  36.     struct timeval tm;
  37.  
  38.     tm.tv_sec = microseconds / 1000000;
  39.     tm.tv_usec = microseconds % 1000000;
  40.  
  41.     return select(0, NULL, NULL, NULL, &tm);
  42. }
  43.  
  44. This will wait for _at_least_ an interval equal to microseconds.
  45.  
  46. In ANSI C, the only way to get finer delays is to call the clock() function and
  47. poll, which is wasteful of CPU time on MT systems.
  48. -- 
  49.  
  50.